/**
 * Created by Jie on 2017/1/14.
 */
//获取url参数值
function getUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); // 构造一个含有目标参数的正则表达式对象
    var r = window.location.search.substr(1).match(reg); // 匹配目标参数
    if (r != null)
        return unescape(r[2]);
    return ""; // 返回参数值
}

// 日期格式转时间戳
function format_date_timestamp(_date) {
    //var timestamp = new Date( _date.replace(/-/g,'/')).getTime();
    var timestamp = Date.parse(new Date(_date));
    return timestamp;
}
// 时间戳转短日期格式
function format_timestamp_short_date(_date) {
    var $date = new Date(parseInt(_date) * 1000);
    var $short_date = $date.getFullYear()+"-"+($date.getMonth()+1)+"-"+$date.getDate();
    return $short_date;
}
function format_timestamp_time(_timestamp) {
    var d = new Date(_timestamp * 1000);    //根据时间戳生成的时间对象
    var date = (d.getFullYear()) + "-" +
        (d.getMonth() + 1) + "-" +
        (d.getDate()) + " " +
        (d.getHours()) + ":" +
        (d.getMinutes()) + ":" +
        (d.getSeconds());
    return date;
}
//检验是否是日期格式 例：2017-01-12
function checkDate(_date) {
    var result = _date.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
    if (result == null){
        return false;
    }else{
        return true;
    }
}
//自定义字符串转json对象
function str2json(action,str){
    var o = {};
    var obj = convertArray(eval("("+str+")"));
    o.data = JSON.stringify(obj);
    o.action = action;
    return o;
}
//form表单自动转json对象
function getFrom(_form) {
    var obj = $(_form).serializeArray();

    return convertArray(obj);
}
//主要是推荐这个函数。它将jquery系列化后的值转为name:value的形式。
function convertArray(o) {
    var v = {};
    for (var i in o) {
        if (o[i].name != '__VIEWSTATE') {
            if (typeof (v[o[i].name]) == 'undefined')
                v[o[i].name] = o[i].value;
            else
                v[o[i].name] += "," + o[i].value;
        }
    }
    return v;
}
//获取长度为len的随机字符串
function random_string(len) {
    len = len || 32;
    var $chars = 'ABCDEFGHJKMNPQRSTWXYZ2345678'; // 默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1
    var maxPos = $chars.length;
    var pwd = '';
    for (i = 0; i < len; i++) {
        pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
    }
    return pwd;
}
//打印日志
function log(_obj) {
    console.log(_obj);
}
//自动校验
function autoRequired(_prefix,_obj) {
    var ok = true;
    for (var key in _obj)
    {
        var id = '#'+_prefix+key;
        var required = $(id).attr("required");
        if(required=="required"){
            var val = _obj[key];
            if($.trim(val).length == 0){
                var placeholder = $(id).attr('placeholder');
                $(id).focus();
                ok = false;
                tips(placeholder);
                break;
            }
        }
    }
    return ok;
}
//设置页面的值
function setHtmlVal(obj) {
    for (var key in obj) {
        var $temId = '#x' + key;
        if ($($temId).length > 0) {
            try {
                $($temId).html(obj[key]);
            } catch (e) {
            }
        }
    }
}
//设置表单值 自定义前缀id
function setFormValByPrefix(obj,prefix) {
    for (var key in obj) {
        var $temId = '#'+prefix + key;
        if ($($temId).length > 0) {
            try {
                $($temId).val(obj[key]);
            } catch (e) {

            }
        }
    }
    mdui.updateTextFields();
}
//设置页面的值 自定义前缀id
function setHtmlVal(obj,prefix) {
    for (var key in obj) {
        var $temId = '#'+prefix + key;
        if ($($temId).length > 0) {
            try {
                $($temId).html(obj[key]);
            } catch (e) {
            }
        }
    }
}
//设置表单值
function setFormVal(_prefix,obj) {
    for (var key in obj) {
        var $temId = '#'+ _prefix + key;
        if ($($temId).length > 0) {
            try {
                $($temId).val(obj[key]);
            } catch (e) {

            }
        }
    }
    mdui.updateTextFields();
}
//生成查询的json
function form2search() {
    var _form = "#search-form";
    var obj = $(_form).serializeArray();
    var searchObj = convertArray(obj);
    var ruleArr = [];

    for (var key in searchObj) {
        var $id = "[name='" + key + "']";
        var $op = $(_form).find($id).attr('data-op');
        if (searchObj[key].length > 0 || searchObj[key] != '' ) {
            if (checkDate(searchObj[key])) {
                ruleArr.push({
                    "field": key,
                    "op": $op,
                    "data": format_date_timestamp(searchObj[key])
                });
            } else {
                if (key == 'key') {
                    ruleArr.push({
                        "field": key,
                        "op": 'like',
                        "data": searchObj[key]
                    });
                } else {
                    ruleArr.push({
                        "field": key,
                        "op": 'like',
                        "data": searchObj[key]
                    });
                }
            }
        }
    }
    return (JSON.stringify(ruleArr));
}
//日期格式检查
function checkDate(_str){
    var a = /^(\d{4})-(\d{2})-(\d{2})$/
    if (!a.test(_str)) {
        return false
    }
    else{
        return true
    }
}
//初始化表单
function initForm() {
    $('input').val('');
    $('select').val(0);
    $('textarea').val('');
}

function toPage(_url) {
    window.location.href = _url;
}